# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time

  include AuthenticatedSystem
  before_filter :set_locale
  
  # Returns true if user is logged in and allowed to create arguments and responses
  def approval_required
    unless logged_in?
      access_denied
    else
      current_user.is_approved? || user_not_approved
    end
  end

  # Returns true if user is logged in and admin
  def admin_required
    unless logged_in?
      access_denied
    else
      current_user.is_admin? || user_not_approved
    end
  end

  # Shows error message and redirects user to main bingo board page
  def user_not_approved
    flash[:error] = "Permission denied".t
    redirect_to root_url
  end

  layout 'application'
  before_filter :set_locale
  def globalize?
    logged_in? && current_user.is_approved && current_user.is_translating
  end
  
  #self.languages = SUPPORTED_LANGUAGES
  #def languages
  #  return self.languages
  #end
  
  # Determines the user's language of choice
  def set_locale
    default_locale = 'en-US'
    request_language = request.env['HTTP_ACCEPT_LANGUAGE']
    request_language = request_language.nil? ? nil : 
      request_language[/[^,;]+/]
  
    @locale = params[:locale] || session[:locale] ||
              request_language || default_locale
    session[:locale] = @locale
    begin
      Locale.set @locale
    rescue
      Locale.set default_locale
    end
  end 
 
 # Keeps a full-page layout from rendering on an Ajax request 
  def render(*args)
    args.first[:layout] = false if request.xhr? and args.first[:layout].nil?
    super
  end

  
  # See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery :secret => '7abd6973e72451e8e47a09821992dd81'
end
